package com.dys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @className: com.dys.controller.JDBCController
 * @description: com.dys.controller
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/11/26 21:30
 */
@RestController
public class JDBCController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/listEmps")
    public List<Map<String, Object>> listEmps() {
        String sql = "select * from t_emp";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @RequestMapping(value = "/addEmp")
    public String addEmp() {
        String sql = "insert into t_emp(name,age) values('雷军',55)";
        int count = jdbcTemplate.update(sql);
        System.out.println("删除受影响行数：" + count);
        return "添加成功";
    }

    @RequestMapping(value = "/updateEmp/{id}")
    public String updateEmp(@PathVariable(value = "id") int id) {
        String sql = "update t_emp set name = ?,age = ? where id = ?";
        String name = "余承东";
        int age = 55;
        int count = jdbcTemplate.update(sql, name, age, id);
        System.out.println("修改受影响行数：" + count);
        return "修改成功";
    }

    @RequestMapping(value = "/deleteEmp/{id}")
    public String deleteEmpById(@PathVariable(value = "id") int id) {
        String sql = "delete from t_emp where id = ?";
        int count = jdbcTemplate.update(sql, id);
        System.out.println("删除受影响行数：" + count);
        return "删除成功";
    }
}
